package org.soderiakelvy.controller.reparto;

import javax.faces.application.FacesMessage;

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.soderiakelvy.controller.util.Constante;
import org.soderiakelvy.controller.util.Util;
import org.soderiakelvy.model.domain.PersonalVO;
import org.soderiakelvy.model.domain.RolVO;
import org.soderiakelvy.model.service.RepartoServicio;
import org.soderiakelvy.model.service.imp.RepartoServicioImp;
import org.soderiakelvy.model.util.ConstantModel;

@ManagedBean
public class PersonalCreate {
	private PersonalVO personal;
	private RepartoServicio service;
	private String cuil;
	private String postCuil;
	private String preCuil;
	private RolVO rol;
	//--- CONSTRUCTOR ---
	public PersonalCreate(){
		service 	=  new RepartoServicioImp();
		personal 	=  new PersonalVO();
		rol 		=  new RolVO();
	}
	
	public String guardarPersonal(PersonalVO personal){

		System.out.println("Nombre: "+personal.getNombre());
		System.out.println(personal.getApellido());
		
		this.personal.setPassword(Util.getMD5(personal.getPassword()));
		this.personal.setCuil(this.getCuil());
		System.out.println("El cuil es "+this.getCuil());
		System.out.println("el pass que guarda: "+ personal.getPassword());
		this.personal.setRol(rol);

		if (service.insertPersonal(personal).equals(ConstantModel.SUCCESS)) {
			return Constante.URL_PERSONAL_LIST;
		} else {
			FacesContext.getCurrentInstance()
					.addMessage(
							null,
							new FacesMessage(FacesMessage.SEVERITY_FATAL,
									"El Nombre de Personal Ya Existe",
									"para continuar "));
			return null;
		}
	}
	
	public String cancelarAltaPersonal(){
		return Constante.URL_PERSONAL_LIST;
	}

	public PersonalVO getPersonal() {
		return personal;
	}

	public void setPersonal(PersonalVO personal) {
		this.personal = personal;
	}

	public String getCuil() {
		this.setCuil(preCuil+getPersonal().getDni()+postCuil);
		return cuil;
	}

	public void setCuil(String cuil) {
		this.cuil = cuil;
	}

	public String getPostCuil() {
		return postCuil;
	}

	public void setPostCuil(String postCuil) {
		this.postCuil = postCuil;
	}

	public String getPreCuil() {
		return preCuil;
	}

	public void setPreCuil(String preCuil) {
		this.preCuil = preCuil;
	}

	public RolVO getRol() {
		return rol;
	}

	public void setRol(RolVO rol) {
		this.rol = rol;
	}
	
}
